<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html class="gr__open-std_org gr__isocpp_org"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style type="text/css">
pre {margin-left:20pt; }
pre > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
pre > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
body { color: #000000; background-color: #FFFFFF; }
del { text-decoration: line-through; color: #8B0040; }
ins { text-decoration: underline; color: #005100; }

p.example { margin-left: 2em; }
pre.example { margin-left: 2em; }
div.example { margin-left: 2em; }

code.extract { background-color: #F5F6A2; }
pre.extract { margin-left: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }

p.function { }
.attribute { margin-left: 2em; }
.attribute dt { float: left; font-style: italic;
  padding-right: 1ex; }
.attribute dd { margin-left: 0em; }

blockquote.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5empadding-right: 0.5em; ; }

blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }

table.header { border: 0px; border-spacing: 0;
  margin-left: 0px; font-style: normal; }

table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none; 
  padding-right: 0.4em; border: none; }
td { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none;
  padding-right: 0.4em; border: none; }
</style>

<title>Fixed-size 'span' construction from dynamic-size range</title>
</head>

<body data-gr-c-s-loaded="true">

<table class="header"><tbody>
  <tr>
   <th>Document number:&nbsp;&nbsp;</th><th> </th><td>D1976R1</td>
  </tr>
  <tr>
    <th>Date:&nbsp;&nbsp;</th><th> </th><td>2020-01-03</td>
  </tr>
  <tr>
    <th>Audience:&nbsp;&nbsp;</th><th> </th><td>Library Working Group</td>
  </tr>
  <tr>
    <th>Reply-to:&nbsp;&nbsp;</th><th> </th><td><address>Tomasz Kamiński &lt;tomaszkam at gmail dot com&gt;</address></td>
  </tr>
</tbody></table>

<h1><a name="title">Fixed-size <code>span</code> construction from dynamic range</a></h1>

<h2><a name="intro">1. Introduction</a></h2>

<p>This paper provides more detailed explanation of <a href="https://github.com/cplusplus/nbballot/issues/246">PL250 NB issue</a>.
   We explore issues with construction of fixed-size <code>span</code> construction from the range
   with the dynamic size. This constructor are source of the undefined behavior, without providing
   any synctatic suggestion on the user side.</p>

<p>To resolve the issues, we present three options:</p>
<ul>
  <li>A. Separate fixed-size and dynamic <code>span</code> (remove fixed-size <code>span</code> for C++20)</li>
  <li>B. Disabling constructors</li>
  <li>C. Making constructors explicit (PL250 proposed resolution)</li>
</ul>

<p>Per LEWG guidance in Belfast, the proposed resolution follows the option C (PL250 guidance) and marks the fixed-spize <code>span</code> constructors
   from dynamic-size range explicit.</p>

<table>
  <tbody><tr>
    <th></th>
    <th>Before</th>
    <th>After</th>
  </tr>

  <tr>
    <td colspan="3">
<pre>void processFixed(std::span&lt;int, 5&gt;);
void processDynamic(std::span&lt;int&gt;);

std::vector&lt;int&gt; v3(3);
std::vector&lt;int&gt; v5(5);
</pre>
	</td>
  </tr>
  
  <tr>
    <td colspan="3">
      <p>Dynamic range with different size (5 vs 3)</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>
processFixed(v3);                                             //
processFixed({v3.data(), v3.data() + 3});                     //

processFixed(span&lt;int, 5&gt;(v3));                               //
processFixed(span&lt;int, 5&gt;{v3.data(), v3.data() + 3});         //

processFixed(span&lt;int&gt;(v3).first&lt;5&gt;());                       //
processFixed(span&lt;int&gt;{v3.data(), v3.data() + 3}.first&lt;5&gt;()); //

span&lt;int, 5&gt; s = v3;                                          //
span&lt;int, 5&gt; s(v3);                                           //

span&lt;int, 5&gt; s = {v3.data(), v3.data() + 3};                  //
span&lt;int, 5&gt; s{v3.data(), v3.data() + 3};                     //
</pre>
    </td>

    <td>
<pre>
<font color="orange">ill-formed</font>
<font color="red">undefined-behavior</font>

<font color="orange">ill-formed</font>
<font color="red">undefined-behavior</font>

<font color="red">undefined-behavior</font>
<font color="red">undefined-behavior</font>

<font color="orange">ill-formed</font>
<font color="red">undefined-behavior</font>

<font color="red">undefined-behavior</font>
<font color="red">undefined-behavior</font></pre>
   </td>

    <td>
<pre>
<font color="orange">ill-formed</font>
<font color="orange">ill-formed</font>

<font color="red">undefined-behavior</font>
<font color="red">undefined-behavior</font>

<font color="red">undefined-behavior</font>
<font color="red">undefined-behavior</font>

<font color="orange">ill-formed</font>
<font color="red">undefined-behavior</font>

<font color="orange">ill-formed</font>
<font color="red">undefined-behavior</font></pre>
   </td>
  </tr>
  
<tr>
    <td colspan="3">
      <p>Dynamic range with matching size (5 vs 5)</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>
processFixed(v5);                                             //
processFixed({v5.data(), v5.data() + 5});                     //

processFixed(span&lt;int, 5&gt;(v5));                               //
processFixed(span&lt;int, 5&gt;{v5.data(), v5.data() + 5});         //

processFixed(span&lt;int&gt;(v5).first&lt;5&gt;());                       //
processFixed(span&lt;int&gt;{v5.data(), v5.data() + 5}.first&lt;5&gt;()); //

span&lt;int, 5&gt; s = v5;                                          //
span&lt;int, 5&gt; s(v5);                                           //

span&lt;int, 5&gt; s = {v5.data(), v5.data() + 5};                  //
span&lt;int, 5&gt; s{v5.data(), v5.data() + 5};                     //
</pre>
    </td>

    <td>
<pre>
<font color="orange">ill-formed</font>
<font color="green">ok</font>

<font color="orange">ill-formed</font>
<font color="green">ok</font>

<font color="green">ok</font>
<font color="green">ok</font>

<font color="orange">ill-formed</font>
<font color="green">ok</font>

<font color="green">ok</font>
<font color="green">ok</font></pre>
   </td>

    <td>
<pre>
<font color="orange">ill-formed</font>
<font color="orange">ill-formed</font>

<font color="green">ok</font>
<font color="green">ok</font>

<font color="green">ok</font>
<font color="green">ok</font>

<font color="orange">ill-formed</font>
<font color="green">ok</font>

<font color="orange">ill-formed</font>
<font color="green">ok</font></pre>
   </td>
  </tr>
  
</tbody></table>
  

<h2><a name="history">2. Revision history</a></h2>

<h3><a name="history.r1">2.2. Revision 1</a></h3>

<ul>
  <li>Added wording for Option C.</li>
  <li>Included Tony Tables for proposed resolution (option C) in Introduction.</li>
  <li>Fixed minor typos in text.</li>
</ul>
   
<h3><a name="history.r0">2.1. Revision 0</a></h3>

<p>Initial revision.</p>
   
<h2><a name="motivation">3. Motivation and Scope</a></h2>

<p>The resolution of <a href="https://wg21.link/lwg3101">the LWG issue 3101</a> prevents user from running
   into accidental undefined-behavior when the <code>span</code> with fixed size is constructed from the
   range with the size that is not known at compile time. To illustrate:</p>
<pre>void processFixed(std::span&lt;int, 5&gt;);

std::vector&lt;int&gt; v;
</pre>

<p>With the above declaration the following invocation is ill-formed:</p>
<pre>processFixed(v); // <font color="orange">ill-formed</font></pre> 
<p>Before the resolution of the issues, the above code was having undefined-behavior if the <code>v.size()</code> was
   different than <code>5</code> (size of <code>span</code> in declaration of <code>processFixed</code>).</p>
   
<p>However, the proposed resolution does not prevent the accidental undefined-behavior in situation when
   <code>(iterator, size)</code> or the <code>(iterator, sentinel)</code> constructor is used:</p>
<pre>void processFixed({v.data(), v.size()}); // <font color="red">undefined-behavior</font> if v.size() != 5
void processFixed({v.begin(), v.end()}); // <font color="red">undefined-behavior</font> if v.size() != 5</pre>

<h3><a name="motivation.remove">3.2. Option A: Separate fixed-size and dynamic-size <code>span</code> (remove fixed-size <code>span</code> for C++20)</a></h3>

<p>One of the option of resolving the issue is to separate the fixed-size and dynamic-size <code>span</code>
   into separate template. As it is to late for the C++20 for the introduction of the new template,
   such change would imply removal of the fixed-size <code>span</code> version of the span
   from the standard.</p>
   
<p>As consequence, the <code>span</code> template would become dynamicly sized, and would accept
   single type as template parameter:</p><p>
</p><pre>template&lt;class T&gt; span;</pre>

<p>Futhermore it would allow us to explore extending <code><i>fixed-span</i></code> construction
   to handle user-defined fixed-size ranges. Currently the standard regonizes only native arrays (<code>T[N]</code>),
   <code>std::array&lt;T, N&gt;</code> and fixed-size <code>std::span&lt;T, N&gt;</code> (where <code>N != std::dynamic_extent</code>)
   as fixed-size range. The appropariate trait was proposed in 
   <a href="https://wg21.link/p1419">A SFINAE-friendly trait to determine the extent of statically sized containers</a>.</p>

<h3><a name="motivation.disabling">3.2. Option B: Disabling constructors</a></h3>

<p>We can follow the direction of <a href="https://wg21.link/lwg3101">the LWG issue 3101</a> and
   disable these constructor from particpating from the overload resolution entirelly. That would
   prevent the constructing the fixed-span from the dynamic range, and require the 
   user to <code>first&lt;N&gt;()</code>/<code>last&lt;N&gt;</code>/<code>subspan&lt;P, N&gt;</code> 
   methods explicitly.</p>   
<pre>void processFixed(std::span(v).first&lt;5&gt;());      // <font color="red">undefined-behavior</font> if v.size() &lt; 5
void processFixed(std::span(v).last&lt;5&gt;());       // <font color="red">undefined-behavior</font> if v.size() &lt; 5
void processFixed(std::span(v).subspan&lt;1, 5&gt;()); // <font color="red">undefined-behavior</font> if v.size() &lt; 6 = 1 + 5</pre>
<p>[ Note: Lack of template parameter for <code>span</code> in above examples is intentional - they use deduction guides. ]</p>
   
<p>Tony Tables for option B.</p>
<table>
  <tbody><tr>
    <th>Before</th>
    <th>After: Option B</th>
  </tr>

  <tr>
    <td colspan="2">
<pre>void processFixed(std::span&lt;int, 5&gt;);
void processDynamic(std::span&lt;int&gt;);</pre>
	</td>
  </tr>
  
  <tr>
    <td colspan="2">
      <p>Dynamic range with different size</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>std::vector&lt;int&gt; v3(3);
processFixed(v3);                                             // <font color="orange">ill-formed</font>
processFixed({v3.data(), v3.data() + 3});                     // <font color="red">undefined-behavior</font>
processFixed({v3.data(), 3});                                 // <font color="red">undefined-behavior</font>

processFixed(span&lt;int, 5&gt;(v3));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v3.data(), v3.data() + 3});         // <font color="red">undefined-behavior</font>
processFixed(span&lt;int, 5&gt;{v3.data(), 3});                     // <font color="red">undefined-behavior</font>

processFixed(span&lt;int&gt;(v3).first&lt;5&gt;());                       // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), v3.data() + 3}.first&lt;5&gt;()); // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), 3}.first&lt;5&gt;());             // <font color="red">undefined-behavior</font></pre>

    </td>

    <td>
<pre> 
processFixed(v3);                                             // <font color="orange">ill-formed</font>
processFixed({v3.data(), v3.data() + 3});                     // <font color="orange">ill-formed</font>
processFixed({v3.data(), 3});                                 // <font color="orange">ill-formed</font>

processFixed(span&lt;int, 5&gt;(v3));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v3.data(), v3.data() + 3});         // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v3.data(), 3});                     // <font color="orange">ill-formed</font>

processFixed(span&lt;int&gt;(v3).first&lt;5&gt;());                       // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), v3.data() + 3}.first&lt;5&gt;()); // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), 3}.first&lt;5&gt;());             // <font color="red">undefined-behavior</font></pre>
   </td>
  </tr>
  
  <tr>
    <td colspan="2">
      <p>Dynamic range with matching size</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>std::vector&lt;int&gt; v5(5);
processFixed(v5);                                             // <font color="orange">ill-formed</font>
processFixed({v5.data(), v5.data() + 5});                     // <font color="green">ok</font>
processFixed({v5.data(), 5});                                 // <font color="green">ok</font>

processFixed(span&lt;int, 5&gt;(v5));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v5.data(), v5.data() + 5});         // <font color="green">ok</font>
processFixed(span&lt;int, 5&gt;{v5.data(), 5});                     // <font color="green">ok</font>

processFixed(span&lt;int&gt;(v5).first&lt;5&gt;());                       // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), v5.data() + 5}.first&lt;5&gt;()); // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), 5}.first&lt;5&gt;());             // <font color="green">ok</font></pre>

    </td>

    <td>
<pre> 
processFixed(v5);                                             // <font color="orange">ill-formed</font>
processFixed({v5.data(), v5.data() + 5});                     // <font color="orange">ill-formed</font>
processFixed({v5.data(), 5});                                 // <font color="orange">ill-formed</font>

processFixed(span&lt;int, 5&gt;(v5));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v5.data(), v5.data() + 5});         // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v5.data(), 5});                     // <font color="orange">ill-formed</font>

processFixed(span&lt;int&gt;(v5).first&lt;5&gt;());                       // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), v5.data() + 5}.first&lt;5&gt;()); // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), 5}.first&lt;5&gt;());             // <font color="green">ok</font></pre>
   </td>
  </tr>
</tbody></table>

<h3><a name="motivation.explicit">3.3. Option C: Making constructors explicit</a></h3>

<p>This is original resolution proposed in <a href="https://github.com/cplusplus/nbballot/issues/246">PL250</a>.</p>

<p>The construction of the fixed-sized <code>span</code> from the dynamicly sized range, is 
   not indentity operation - this operation assumes additional semantic property of the type
   (size of the range). Such conversion between semantically different types, should not be
   implicit. We can resolve the problem, by marking all of such constructor explicit, as follows:</p>

<table>
  <tbody><tr>
    <td>Destination/Source</td>
	<th>Fixed</th>
	<th>Dynamic</th>
  </tr>
  
  <tr>
    <th>Fixed</th>
	<td>implicit (ill-formed if source.size() != dest.size())</td>
	<td>explicit (undefined-behavior if source.size() != dest.size())</td>
  </tr>
  
  <tr>
    <th>Dynamic</th>
	<td>implicit (always ok)</td>
	<td>implicit (always ok)</td>
  </tr>
</tbody></table>

<p>Tony Tables for option C.</p>
<table>
  <tbody><tr>
    <th>Before</th>
    <th>After: Option C</th>
  </tr>

  <tr>
    <td colspan="2">
<pre>void processFixed(std::span&lt;int, 5&gt;);
void processDynamic(std::span&lt;int&gt;);</pre>
	</td>
  </tr>
  
  <tr>
    <td colspan="2">
      <p>Dynamic range with different size</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>std::vector&lt;int&gt; v3(3);
processFixed(v3);                                             // <font color="orange">ill-formed</font>
processFixed({v3.data(), v3.data() + 3});                     // <font color="red">undefined-behavior</font>
processFixed({v3.data(), 3});                                 // <font color="red">undefined-behavior</font>

processFixed(span&lt;int, 5&gt;(v3));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v3.data(), v3.data() + 3});         // <font color="red">undefined-behavior</font>
processFixed(span&lt;int, 5&gt;{v3.data(), 3});                     // <font color="red">undefined-behavior</font>

processFixed(span&lt;int&gt;(v3).first&lt;5&gt;());                       // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), v3.data() + 3}.first&lt;5&gt;()); // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), 3}.first&lt;5&gt;());             // <font color="red">undefined-behavior</font></pre>
    </td>

    <td>
<pre> 
processFixed(v3);                                             // <font color="orange">ill-formed</font>
processFixed({v3.data(), v3.data() + 3});                     // <font color="orange">ill-formed</font>
processFixed({v3.data(), 3});                                 // <font color="orange">ill-formed</font>

processFixed(span&lt;int, 5&gt;(v3));                               // <font color="red">undefined-behavior</font>
processFixed(span&lt;int, 5&gt;{v3.data(), v3.data() + 3});         // <font color="red">undefined-behavior</font>
processFixed(span&lt;int, 5&gt;{v3.data(), 3});                     // <font color="red">undefined-behavior</font>

processFixed(span&lt;int&gt;(v3).first&lt;5&gt;());                       // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), v3.data() + 3}.first&lt;5&gt;()); // <font color="red">undefined-behavior</font>
processFixed(span&lt;int&gt;{v3.data(), 3}.first&lt;5&gt;());             // <font color="red">undefined-behavior</font></pre>
   </td>
  </tr>
  
  <tr>
    <td colspan="2">
      <p>Dynamic range with matching size</p>
	</td>
  </tr>
  
  <tr>
    <td>
<pre>std::vector&lt;int&gt; v5(5);
processFixed(v5);                                             // <font color="orange">ill-formed</font>
processFixed({v5.data(), v5.data() + 5});                     // <font color="green">ok</font>
processFixed({v5.data(), 5});                                 // <font color="green">ok</font>

processFixed(span&lt;int, 5&gt;(v5));                               // <font color="orange">ill-formed</font>
processFixed(span&lt;int, 5&gt;{v5.data(), v5.data() + 5});         // <font color="green">ok</font>
processFixed(span&lt;int, 5&gt;{v5.data(), 5});                     // <font color="green">ok</font>

processFixed(span&lt;int&gt;(v5).first&lt;5&gt;());                       // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), v5.data() + 5}.first&lt;5&gt;()); // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), 5}.first&lt;5&gt;());             // <font color="green">ok</font></pre>
    </td>

    <td>
<pre> 
processFixed(v5);                                             // <font color="orange">ill-formed</font>
processFixed({v5.data(), v5.data() + 5});                     // <font color="orange">ill-formed</font>
processFixed({v5.data(), 5});                                 // <font color="orange">ill-formed</font>

processFixed(span&lt;int, 5&gt;(v5));                               // <font color="green">ok</font>
processFixed(span&lt;int, 5&gt;{v5.data(), v5.data() + 5});         // <font color="green">ok</font>
processFixed(span&lt;int, 5&gt;{v5.data(), 5});                     // <font color="green">ok</font>

processFixed(span&lt;int&gt;(v5).first&lt;5&gt;());                       // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), v5.data() + 5}.first&lt;5&gt;()); // <font color="green">ok</font>
processFixed(span&lt;int&gt;{v5.data(), 5}.first&lt;5&gt;());             // <font color="green">ok</font></pre>
   </td>
  </tr>
</tbody></table>

<h3><a name="motivation.dynamic">3.4. No impact on dynamic-sized span</a></h3>
 
<p>All proposed options (including removal) does not have any impact on the construction of the
   dynamic-sized span (i.e. <code>span&lt;T&gt;</code>). The construction changes affect only
   cases when <code>N != std::dynamic_extent</code>.</p>

   
<h3><a name="motivation.initialization">3.5. Option B vs C: Impact on initialization</a></h3>

<p>The major difference between the option B and option C, is the impact the impact on the initialization
   of the span variables. Some of the readers, may consider the difference between various syntaxes
   and their meaning two subtle.</p>
   
<p>Tony Tables for initialization.</p>
<table>
  <tbody><tr>
    <th>Option B</th>
    <th>Option C</th>
  </tr>
  
  <tr>
    <td>
<pre>std::vector&lt;int&gt; v3(3);

span&lt;int, 5&gt; s = v3;                             // <font color="orange">ill-formed</font>
span&lt;int, 5&gt; s(v3);                              // <font color="orange">ill-formed</font>
auto s = span&lt;int, 5&gt;(v3);                       // <font color="orange">ill-formed</font>

span&lt;int, 5&gt; s = {v3.data(), v3.data() + 3};     // <font color="orange">ill-formed</font>
span&lt;int, 5&gt; s{v3.data(), v3.data() + 3};        // <font color="orange">ill-formed</font>
auto s = span&lt;int, 5&gt;{v3.data(), v3.data() + 3}; // <font color="orange">ill-formed</font></pre>
    </td>

    <td>
<pre> 
 
span&lt;int, 5&gt; s = v3;                             // <font color="orange">ill-formed</font>
span&lt;int, 5&gt; s(v3);                              // <font color="red">undefined-behavior</font>
auto s = span&lt;int, 5&gt;(v3);                       // <font color="red">undefined-behavior</font>

span&lt;int, 5&gt; s = {v3.data(), v3.data() + 3};     // <font color="orange">ill-formed</font>
span&lt;int, 5&gt; s{v3.data(), v3.data() + 3};        // <font color="red">undefined-behavior</font>
auto s = span&lt;int, 5&gt;{v3.data(), v3.data() + 3}; // <font color="red">undefined-behavior</font></pre>
   </td>
  </tr>

</tbody></table>

<h3><a name="motivation.fixed-sized">3.6. Option B and C: Construction from fixed-sized range</a></h3>
 
<p>Neither option B nor C, proposes any change to the behavior of the construction of
   the fixed-size <code>span</code> from the ranges that are recognized by the
   standard as fixed-size: native arrays (<code>T[N]</code>),
   <code>std::array&lt;T, N&gt;</code> and fixed-size <code>std::span&lt;T, N&gt;</code> (where <code>N != std::dynamic_extent</code>).
   The construction is implicit if size of the source is the same as the size of destination,
   ill-formed otherwise.</p>

<pre>void processFixed(span&lt;int, 5&gt;);

std::array&lt;int, 3&gt; a3;
std::array&lt;int, 5&gt; a5;

processFixed(a3); // <font color="orange">ill-formed</font>
processFixed(a5); // <font color="green">ok</font>

std::span&lt;int, 3&gt; s3(a3);
std::span&lt;int, 5&gt; s5(a5);

processFixed(s3); // <font color="orange">ill-formed</font>
processFixed(s5); // <font color="green">ok</font></pre> 

<h3><a name="motivation.p1394">3.7. Option B and C: Impact of the P1394</a></h3>
  
<p>The <a href="https://wg21.link/p1394">P1394: Range constructor for <code>std::span</code></a>
   (that is targeting C++20) generalized the constructor of the span.</p>
   
<p>The <code>Container</code> constructor was replaced with the <code>Range</code> constructor,
   that have the same constrain (i.e. it is disabled for fixed-size <code>span</code>),
   so the original example remain ill-formed:</p>
<pre>processFixed(v); // <font color="orange">ill-formed</font></pre> 
 
<p>In addition it replaces the <code>(pointer, size)</code> and <code>(pointer, pointer)</code>
   constructor, with more general <code>(iterator, size)</code> and <code>(iterator, sentinel)</code>.
   As consequence in addition the undefined-behavior is exposed in more situations:</p>
<pre>void processFixed({v.begin(), v.size()}); // <font color="red">undefined-behavior</font> if v.size() != 5
void processFixed({v.begin(), v.end()});  // <font color="red">undefined-behavior</font> if v.size() != 5</pre>
<p>in addition to:</p>
<pre>void processFixed({v.data(), v.size()});            // <font color="red">undefined-behavior</font> if v.size() != 5
void processFixed({v.data(), v.data() + v.size()}); // <font color="red">undefined-behavior</font> if v.size() != 5</pre>
 
<p>Changes presented in this paper still apply after signature changes from P1394.</p>

<h2><a name="impact">4. Impact and Implementability</a></h2>

<p>As the <code>std:span</code> was introduced in C++20, the changes introduce in these paper (regardless of the selected option)
   cannot break existing code. In addition, all presented options do not affect uses of <code>span</code> with the dynamic size.</p>
   
<p>The implementation of the option B requires duplicating a constrain:<br>
   Constrains: <code>extent == dynamic_extent</code> is <code>true</code>.<br>
   that is already present in <code>Container</code>/<code>Range</code> constructor
   (<a href="http://eel.is/c++draft/span.cons#14.1">[span.cons] p14.1</a>) to 3 additional constuctors.
   In can be implemented using the SFINAE tricks (<code>std::enable_if</code>) or <code>requires</code> clause.</p>

<p>The implementation of the option C mostly requires adding an conditional explicit
   specifier to 4 constuctors:</p>
<pre>explicit(extent != dynamic_extent)</pre>
   
<h2><a name="wording">5. Proposed Wording</a></h2>

<p>The proposed wording changes refer to <a href="http://wg21.link/n4842"></a> N4842 (C++ Working Draft, 2019-11-27).</p>

<p>Apply following changes to section [span.overview] Overview:</p>
<blockquote class="std">
<pre>
   // [span.cons], constructors, copy, and assignment
   constexpr span() noexcept;
   template&lt;class It&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(It first, size_type count);
   template&lt;class It, class End&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(It first, End last);
   template&lt;size_t N&gt; 
     constexpr span(element_type (&arr)[N]) noexcept;
   template&lt;size_t N&gt; 
     constexpr span(array&lt;value_type, N&gt;& arr) noexcept;
   template&lt;size_t N&gt; 
     constexpr span(const array&lt;value_type, N&gt;& arr) noexcept;
   template&lt;class R&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(R&amp;&amp; r);
   constexpr span(const span& other) noexcept = default;
   template&lt;class OtherElementType, size_t OtherExtent&gt;
     constexpr <ins>explicit(<em>see below</em>)</ins> span(const span&lt;OtherElementType, OtherExtent&gt;&amp; s) noexcept;</pre>
</pre>
</blockquote>


<p>Apply following changes to section [span.cons] Constructors, copy, and assignment:</p>
<blockquote class="std">
  
  <pre>   template&lt;class It&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(It first, size_type count);</pre>

  <dl class="attribute">

    <dt>Constraints:</dt>
    <dd>Let <code>U</code> be <code>remove_reference_t&lt;iter_reference_t&lt;It&gt;&gt;</code>.
    <ul>
      <li><code>It</code> satisfies <code>contiguous_iterator</code>.</li>
      <li><code>is_convertible_v&lt;U(*)[], element_type(*)[]&gt;</code> is <code>true</code>. 
	      [Note: The intent is to allow only qualification conversions of the iterator reference type to <code>element_type</code>. — end note]</li>
    </ul>
    <p></p></dd>

    <dt>Preconditions:</dt>
    <dd>&nbsp;
    <ul>
      <li><code>[first, first + count)</code> is a valid range.</li>
      <li><code>It</code> models <code>contiguous_iterator</code>.</li>
	  <li>If <code>extent</code> is not equal to <code>dynamic_extent</code>, then <code>count</code> is equal to <code>extent</code>.
    </ul>
    <p></p></dd>

    <dt>Effects:</dt>
    <dd>Initializes <code>data_</code> with <code>to_address(first)</code> and <code>size_</code> with <code>count</code>.<p></p></dd>

    <dt>Throws:</dt>
    <dd>When and what <code>to_address(first)</code> throws.<p></p></dd>
  </dl>
  
  <pre>   template&lt;class It, class End&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(It first, End last);</pre>

  <dl class="attribute">

    <dt>Constraints:</dt>
    <dd>Let <code>U</code> be <code>remove_reference_t&lt;iter_reference_t&lt;It&gt;&gt;</code>.
    <ul>
      <li><code>is_convertible_v&lt;U(*)[], element_type(*)[]&gt;</code> is <code>true</code>. 
	      [Note: The intent is to allow only qualification conversions of the iterator reference type to <code>element_type</code>. — end note]</li>
      <li><code>It</code> satisfies <code>contiguous_iterator</code>.</li>
	  <li><code>End</code> satisfies <code>sized_sentinel_for&lt;It&gt;</code>.</li>
	  <li><code>is_convertible_v&lt;End, size_t&gt;</code> is <code>false</code>.</li>
    </ul>
    <p></p></dd>

    <dt>Preconditions:</dt>
    <dd>&nbsp;
    <ul>
	  <li>If <code>extent</code> is not equal to <code>dynamic_extent</code>, then <code>last - first</code> is equal to <code>extent</code>.</li>
      <li><code>[first, first + count)</code> is a valid range.</li>
      <li><code>It</code> models <code>contiguous_iterator</code>.</li>
	  <li><code>End</code> models <code>sized_sentinel_for&lt;It&gt;</code>.</li>
    </ul>
    <p></p></dd>

    <dt>Effects:</dt>
    <dd>Initializes <code>data_</code> with <code>to_address(first)</code> and <code>size_</code> with <code>last - first</code>.<p></p></dd>

    <dt>Throws:</dt>
    <dd>When and what <code>to_address(first)</code> throws.<p></p></dd>
  </dl>
  
<pre>   template&lt;size_t N&gt; constexpr span(element_type (&arr)[N]) noexcept;
   template&lt;size_t N&gt; constexpr span(array&lt;value_type, N&gt;& arr) noexcept;
   template&lt;size_t N&gt; constexpr span(const array&lt;value_type, N&gt;& arr) noexcept;</pre>

  <dl class="attribute">
    <dt>Constraints:</dt>
    <dd><ins>Let <code>U</code> be <code>remove_pointer_t&lt;decltype(data(arr))&gt;</code>.</ins>
    <ul>
	  <li><code>extent == dynamic_extent || N == extent</code> is <code>true</code>, and</li>
	  <li><del><code>remove_pointer_t&lt;decltype(data(arr))&gt;(*)[]</code> is convertible to <code>ElementType(*)[]</code>.</del></li>
      <li><ins><code>is_convertible_v&lt;U(*)[], element_type(*)[]&gt;</code> is <code>true</code>.
	      [Note: The intent is to allow only qualification conversions of the iterator reference type to <code>element_type</code>. — end note]</ins></li>
	</ul>
    <p></p></dd>

    <dt>Effects:</dt>
    <dd>Constructs a <code>span</code> that is a view over the supplied array..<p></p></dd>

    <dt>Postconditions:</dt>
    <dd><code>size() == N && data() == data(arr)</code><p></p></dd>
  </dl>
  
<pre>   template&lt;class R&gt;
     constexpr <ins>explicit(extent != dynamic_extent)</ins> span(R&amp;&amp; r);</pre>

  <dl class="attribute">

    <dt>Constraints:</dt>
    <dd>Let <code>U</code> be <code>remove_reference_t&lt;ranges::range_reference_t&lt;R&gt;&gt;</code>.
    <ul>
	  <li><del><code>extent == dynamic_extent</code> is <code>true</code>.</del></li>
	  <li><code>R</code> satisfies <code>ranges::contiguous_range</code> and <code>ranges::sized_range</code>.</li>
	  <li>Either <code>R</code> satisfies <code>safe_range</code> or <code>is_const_v&lt;element_type&gt;</code> is <code>true</code>.</li>
	  <li><code>remove_cvref_t&lt;R&gt;</code> is not a specialization of <code>span</code>.</li>
	  <li><code>remove_cvref_t&lt;R&gt;</code> is not a specialization of <code>array</code>.</li>
	  <li><code>is_array_v&lt;remove_cvref_t&lt;R&gt;&gt;</code> is <code>false</code>.</li>
      <li><code>is_convertible_v&lt;U(*)[], element_type(*)[]&gt;</code> is <code>true</code>.
	      [Note: The intent is to allow only qualification conversions of the iterator reference type to <code>element_type</code>. — end note]</li>
    </ul>
    <p></p></dd>

    <dt>Preconditions:</dt>
    <dd>&nbsp;
    <ul>
	  <li><ins>If <code>extent</code> is not equal to <code>dynamic_extent</code>, then <code>ranges::size(r)</code> is equal to <code>extent</code>.</ins></li>
      <li><code>R</code> models <code>ranges::contiguous_range</code> and <code>ranges::sized_range</code>.</li>
	  <li>If <code>is_const_v&lt;element_type&gt;</code> is <code>false</code>, <code>R</code> models <code>safe_range</code>.</li>
    </ul>
    <p></p></dd>

    <dt>Effects:</dt>
    <dd>Initializes <code>data_</code> with <code>ranges::data(r)</code> and <code>size_</code> with <code>ranges::size(r)</code>.<p></p></dd>

    <dt>Throws:</dt>
    <dd>What and when <code>ranges::data(r)</code> and <code>ranges::size(r)</code> throws.<p></p></dd>
  </dl>
  
[...]
  
<pre>   template&lt;class OtherElementType, size_t OtherExtent&gt;
     constexpr <ins>explicit(<em>see below</em>)</ins> span(const span&lt;OtherElementType, OtherExtent&gt;&amp; s) noexcept;</pre>

  <dl class="attribute">

    <dt>Constraints:</dt>
    <dd>&nbsp;
    <ul>
	  <li><code><del>Extent</del><ins>extent</ins> == dynamic_extent 
	      <ins>|| OtherExtent == dynamic_extent</ins> 
		  || <del>Extent</del><ins>extent</ins> == OtherExtent</code> is <code>true</code>, and</li>
	  <li><del><code>OtherElementType(*)[]</code> is convertible to <code>ElementType(*)[]</code>.</del></li>
      <li><ins><code>is_convertible_v&lt;OtherElementType(*)[], element_type(*)[]&gt;</code> is <code>true</code>.
	      [Note: The intent is to allow only qualification conversions of the iterator reference type to <code>element_type</code>. — end note]</ins></li>
	</ul>
    <p></p></dd>

    <dt><ins>Preconditions</ins>:</dt>
    <dd><ins>If <code>extent</code> is not equal to <code>dynamic_extent</code>, then <code>s.size()</code> is equal to <code>extent</code>.</ins></li>
    <p></p></dd>

    <dt>Effects:</dt>
    <dd>Constructs a <code>span</code> that is a view over the range <code>[s.data(), s.data() + s.size())</code>.<p></p></dd>

    <dt>Postconditions:</dt>
    <dd><code>size() == other.size() && data() == other.data()</code><p></p></dd>

    <dt><ins>Remarks</ins>:</dt>
    <dd><ins>The expression inside <code>explicit</code> is equivalent to: <code>extent != dynamic_extent && OtherExtent == dynamic_extent</code>.</ins><p></p></dd>
  </dl>  
  
</blockquote>

<p>Update the value of the <code>__cpp_lib_span</code> in [version.syn] Header <code>&lt;version&gt;</code> synopsis to reflect the date of approval of this proposal.</p>


<h2><a name="acknowledgements">6. Acknowledgements</a></h2>

<p>Andrzej Krzemieński, Casey Carter, Tim Song and Jeff Garland offered many useful suggestions and corrections to the proposal.</p>

<p>Special thanks and recognition goes to Sabre (<a href="http://www.sabre.com/">http://www.sabre.com</a>) for supporting the production of this proposal
   and author's participation in standardization committee.</p>
   
<h2><a name="literature">7. References</a></h2>

<ol>
  <li>Poland,
      "PL250 22.07.3.2 [span.cons] size mismatch for fixed-sized span",
	  (PL250, <a href="https://github.com/cplusplus/nbballot/issues/246">https://github.com/cplusplus/nbballot/issues/246</a>)</li>

  <li>Stephan T. Lavavej,
      "span's Container constructors need another constraint",
	  (LWG3103, <a href="https://wg21.link/lwg3101">https://wg21.link/lwg3103</a>)</li>
		
  <li>Corentin Jabot, Casey Carter,
      "Range constructor for <code>std::span</code>",
	  (P1394R4, <a href="https://wg21.link/p1394r4">https://wg21.link/p1394r4</a>)</li>
		 
  <li>Corentin Jabot, Casey Carter,
      "A SFINAE-friendly trait to determine the extent of statically sized containers"
      (P1419R0, <a href="https://wg21.link/p1419r0">https://wg21.link/p1419r0</a>)</li>
	  
  <li>Richard Smith,
      "Working Draft, Standard for Programming Language C++"
      (N4842, <a href="https://wg21.link/n4842">https://wg21.link/n4842</a>)</li>

</ol>


</body></html>
